package com.StringBuilder;

/**
 * @Author 小闽
 * @Date 2024/10/5 11:18
 * @PackageName: com.StringBuilder
 * @ClassName: StringBuilderVSStringBufferVSString
 * @Description: String三者比较
 * @Version 1.0
 */
public class StringBuilderVSStringBufferVSString {
    public static void main(String[] args) {
        String text = "";
        long startTime = 0L;
        long endTime = 0l;
        StringBuffer buffer = new StringBuffer("");
        StringBuilder builder = new StringBuilder("");
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 20000; i++) {
            buffer.append(String.valueOf(i));
        };
        endTime = System.currentTimeMillis();
        System.out.println("StringBuffer的执行时间：" + (endTime-startTime));
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 20000; i++) {
            builder.append(String.valueOf(i));
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuilder的执行时间：" + (endTime-startTime));
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 20000; i++) {
            text = text + i;
        }
        endTime = System.currentTimeMillis();
        System.out.println("String的执行时间：" + (endTime-startTime));
    }
}
